global n k casenum kmax draws
% This file conducts the logistic GMM estimation on turnout for the PRI,
% and the strongest opposition party taking abstention as baseline USING AVERAGE
% REMITTANCES AS TREATMENT.
% Tobias Pfutze, May 2009

% Upload Data in order: pripart oppmaxpart remit_percap_intl
% dist_border cycleB1 cycleB2 zona_norte labinc_perhh_log
% labinc_perhh_stdev illiterate indig margmuni00 poptot_2000 unempl
% raildistjuarez_05 histmig24_edoprop00 oppwin_since80 pripart1 oppmaxpart1

RAW=load('/Users/Diana/Dropbox/Research/Participation/Matlab/Data/participationAvremit_adjall.txt');
[N,M]=size(RAW);

% Replace total population by its logged value and av. remittances per
% MXP100
RAW(:,13)=log(RAW(:,13));
RAW(:,3)=RAW(:,3)/100;
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start by creating single random vector for bootstrap %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
draws=10000;
rand('state',1812);
unifrv=rand(draws,1);

% Create two separate matrices conditional on whether municipality has 
% had an opposition party in office since 1980
s0=1;
s1=1;
for i=1:N
    if (RAW(i,17)==0)
        RAW0(s0,1:M)=RAW(i,1:M);
        s0=s0+1;
    elseif RAW(i,17)==1
        RAW1(s1,1:M)=RAW(i,1:M);
        s1=s1+1;
    end
end
[N0,M0]=size(RAW0);
[N1,M1]=size(RAW1);


% Create vectors with randomly selected observations for bootstrap
RAND=ceil(N*unifrv);
RAND0=ceil(N0*unifrv);
RAND1=ceil(N1*unifrv);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create all matrices of dep., indep. and instr. variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create matrices and vectors 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Dependent variable are log odds ratios
y=log(RAW(:,1:2)./(kron(ones(1,2),(1-RAW(:,1)-RAW(:,2)))));
y0=log(RAW0(:,1:2)./(kron(ones(1,2),(1-RAW0(:,1)-RAW0(:,2)))));       
y1=log(RAW1(:,1:2)./(kron(ones(1,2),(1-RAW1(:,1)-RAW1(:,2)))));

x1=[ones(N,1) RAW(:,3:7)];                               
x2=[ones(N,1) RAW(:,3:14)];  

x01=[ones(N0,1) RAW0(:,3:7)];                               
x02=[ones(N0,1) RAW0(:,3:14)];  

x11=[ones(N1,1) RAW1(:,3:7)];                               
x12=[ones(N1,1) RAW1(:,3:14)]; 




%%%%%%%%%%%%%%%%%%%
% DEFINE MATRICES %
%%%%%%%%%%%%%%%%%%%

MEAN1=[mean(x1(:,1:3)) 0 0 0];
MEAN2=[mean(x2(:,1:3)) 0 0 0  mean(x2(:,7:13))];


%Matrices for point estimates and significance tests
[nmax,kmax]=size(x2);

BETA=zeros(2*kmax,6);    % betas for shares
F_WALD=zeros(3,6);
T_WALD=zeros(2*kmax,6);
TJ_WALD=zeros(kmax,6);
PARTIAL=zeros(2*(kmax-1),6);
F_DOF=zeros(1,6);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% PARTICIPATION RESULTS %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ENTIRE SAMPLE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=1;
[n,k] = size(x1);
mean = MEAN1;
Y=(reshape(y',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x1(i,:));
end

[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND, n, k);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x2);
mean = MEAN2;
Y=(reshape(y',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x2(i,:));
end


[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND, n, k);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%   ALWAYS PRI   %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(x01);
mean = MEAN1;
Y=(reshape(y0',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x01(i,:));
end

[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND0, n, k);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x02);
mean = MEAN2;
Y=(reshape(y0',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x02(i,:));
end


[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND0, n, k);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ALREADY OPPOSITION %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(x11);
mean = MEAN1;
Y=(reshape(y1',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x11(i,:));
end

[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND1, n, k);



% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x12);
mean = MEAN2;
Y=(reshape(y1',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x12(i,:));
end


[beta, T, T_J, F, part] = SURPartiesRobust_LogGMM(Y, X, mean, RAND1, n, k);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
F_DOF(:,casenum)=k;
clear Y X beta F T T_J part mean;






%%%%%%%%%%%%%%%%%%%
% Compute p-values%
%%%%%%%%%%%%%%%%%%%

PVAL_TWALD=1-chi2cdf(T_WALD.^2,1);
PVAL_TJWALD=1-chi2cdf(TJ_WALD,3);
FDOF_ALL=kron([2; 1; 1],F_DOF);
PVAL_FWALD=1-chi2cdf(F_WALD,FDOF_ALL);






save ParticipPartiesAdjMaxAvRemit_LogGMMResults BETA PARTIAL PVAL_TWALD PVAL_TJWALD PVAL_FWALD T_WALD TJ_WALD F_WALD;


